﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Common.Util
{
    public class CollectionHelper
    {
        /// <summary>
        /// Делит коллекцию на группы
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable"></param>
        /// <param name="groupSize"></param>
        /// <returns></returns>
        public static IEnumerable<List<T>> ToGroups<T>(
            IEnumerable<T> enumerable, int groupSize)
        {
            List<T> result = new List<T>();
            foreach (var item in enumerable)
            {
                result.Add(item);
                if (result.Count == groupSize)
                {
                    yield return result;
                    result = new List<T>();
                }
            }
            if (result.Count > 0)
            {
                yield return result;
            }
        }
    }
}
